package cn.edu.ctbu.demo1.webapi;
import cn.edu.ctbu.demo1.core.PageUtils;
import cn.edu.ctbu.demo1.core.RUtil;
import cn.edu.ctbu.demo1.domain.Student;
import cn.edu.ctbu.demo1.domain.Teacher;
import cn.edu.ctbu.demo1.service.TeacherService;
import cn.edu.ctbu.demo1.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/webapi/teacher")
public class TeacherRestController {
    @Autowired
    TeacherService teacherService;
    /**
     * 读取的url:/webapi/teacher/get/id
     * @return
     */
    @GetMapping("/get/{id}")
    public R get(@PathVariable Integer id){
        Teacher teacher = teacherService.findById(id);
        return RUtil.success(teacher) ;
    }

    /**
     * 新增学生的方法
     * @param teacher
     * @return
     */
    @PostMapping("/insert")
    public R insert(Teacher teacher){

        Teacher teacher1 = teacherService.insert(teacher);
        return RUtil.success(teacher1);
    }

    /**
     * 更新老师的方法
     * @param teacher
     * @return
     */
    @PutMapping ("/update")
    public R update(Teacher teacher){
        //读取旧的数据
        Teacher oldteacher = teacherService.findById(teacher.getId());
        if(StringUtils.isEmpty(teacher.getPassword())){
            //没有新密码，就是旧密码
            teacher.setPassword(oldteacher.getPassword());
        }
        teacherService.update(teacher);
        return RUtil.success(teacher);
    }
    /**
     * 删除学生的方法
     * @param id
     * @deturn
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Integer id){
        teacherService.delete(id);
        return RUtil.success(null);
    }
//    @GetMapping("/list1")
//    public List<teacher> findByName(String name){
//
//        System.out.println(teacher);
//        return teacher;
//    }

    /**
     * 分页,读取全部内容
     * @return
     */
    @GetMapping("/getbypage")
    public R getByPage(
            @RequestParam(value = "page",defaultValue ="0")Integer page,
            @RequestParam(value = "size",defaultValue ="10")Integer size,
            @RequestParam(value ="select",defaultValue = "")Integer select,
            @RequestParam(value ="content",defaultValue = "")String content
    ){
        Page<Teacher> teacherPage;
        /**
         * 如果没有查询就显示表中的所有内容
         */
        if(StringUtils.isEmpty((content))){
            Pageable  pageable = PageRequest.of(page,size,Sort.by(Sort.Direction.ASC,"id"));
            teacherPage = teacherService.findAll(pageable);
        }
        else {
            Teacher teacher = new Teacher();
            Pageable pageable = PageRequest.of(0,10, Sort.by(Sort.Direction.ASC,"id"));
            ExampleMatcher matcher;
            /**
             * 如果下拉框选中的是学号
             */
            if(select.equals(1))
            {
                teacher.setNo(content);
                matcher = ExampleMatcher.matching().
                        withMatcher("no",ExampleMatcher.GenericPropertyMatchers.contains());
            }
            /**
             * 如果下拉框选中的是姓名
             */
            else {
                teacher.setName(content);
                matcher = ExampleMatcher.matching()//模糊查询
                        .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains());
            }

            Example<Teacher> example = Example.of(teacher,matcher);
            teacherPage = teacherService.findAll(example,pageable);
        }
        PageUtils pageUtils = new PageUtils(teacherPage.getContent(),teacherPage.getTotalElements());
        return RUtil.success(pageUtils);
    }

    /**
     * 获取教师个人信息登陆
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/get")
    public R getTeacher(HttpServletRequest httpServletRequest){
        HttpSession session = httpServletRequest.getSession();
        Integer id = (Integer)session.getAttribute("teacherId");
        Teacher teacher = teacherService.findById(id);
        return RUtil.success(teacher);
    }
}


